﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace YArchitech.Temp
{
	public class HYBranch : List<HYPipeline>, IHYXmlSerial
	{
		public int Index { get; set; }

		public string ShowName
		{
			get
			{
				return "分支" + this.Index.ToString();
			}
		}

		public HYMEPObject Terminal
		{
			get
			{
				return this.m_Terminal;
			}
			set
			{
				this.m_Terminal = value;
				this.m_TerminalId = value.Id;
			}
		}

		public double Length
		{
			get
			{
				return this.m_Length;
			}
		}

		public double Resistance
		{
			get
			{
				return this.m_Resistance;
			}
		}

		public double UnbalanceRate
		{
			get
			{
				return this.m_UnbalanceRate;
			}
		}

		public string GetShowPathString()
		{
			string text = string.Empty;
			if (base.Count == 0)
			{
				return text;
			}
			foreach (HYPipeline hypipeline in this)
			{
				text += hypipeline.ShowString;
				text += this.Connect;
			}
			return text.Substring(0, text.Length - 1);
		}

		public string GetShowIndexString()
		{
			string text = "";
			foreach (HYPipeline hypipeline in this)
			{
				text += hypipeline.Index.ToString();
				text += this.Connect;
			}
			return text.Substring(0, text.Length - this.Connect.Length);
		}

		public virtual void AddFirstObject(HYPipeline pipeLine)
		{
			base.Insert(0, pipeLine);
			this.m_Length += pipeLine.Length;
			this.m_Resistance += pipeLine.Resistance;
		}

		public virtual void ResetPipelineIndex(ref int index)
		{
			foreach (HYPipeline hypipeline in this)
			{
				if (hypipeline.Index == 0)
				{
					hypipeline.Index = index;
					index++;
				}
			}
		}

		public double CalUnbalanceRate(double referenceResistance)
		{
			this.m_UnbalanceRate = 1.0 - this.Resistance / referenceResistance;
			return this.m_UnbalanceRate;
		}

		public virtual void Cal()
		{
			this.m_Length = 0.0;
			this.m_Resistance = 0.0;
			foreach (HYPipeline hypipeline in this)
			{
				this.m_Length += hypipeline.Length;
				this.m_Resistance += hypipeline.Resistance;
			}
		}

		public void WriteXml(XElement xe)
		{
			foreach (HYPipeline hypipeline in this)
			{
				XElement xelement = new XElement("Node");
				xelement.SetValue(hypipeline.Id);
				xe.Add(xelement);
			}
			if (this.Terminal != null)
			{
				XElement xelement2 = new XElement("Terminal");
				xelement2.SetValue(this.Terminal.Id);
				xe.Add(xelement2);
			}
		}

		public virtual void ReadXml(XElement xe)
		{
			if (this.m_PipelineIds == null)
			{
				this.m_PipelineIds = new List<int>();
			}
			this.m_PipelineIds.Clear();
			foreach (XElement element in xe.Elements("Node"))
			{
				int item = (int)element;
				this.m_PipelineIds.Add(item);
			}
			XElement xelement = xe.Element("Terminal");
			if (xelement != null)
			{
				this.m_TerminalId = (int)xelement;
			}
		}

		public void BuildBranchByIndex(HYSystem system)
		{
			base.Clear();
			foreach (int id in this.m_PipelineIds)
			{
				HYPipeline item = system.FindObjectById(id) as HYPipeline;
				base.Add(item);
			}
			this.m_Terminal = system.FindObjectById(this.m_TerminalId);
		}

		public static HYBranch BuildBranch(HYMEPObject terminal, ICollection<HYMEPObject> pipelines)
		{
			HYBranch hybranch = new HYBranch();
			hybranch.Terminal = terminal;
			for (HYMEPObject previous = terminal.Previous; previous != null; previous = previous.Previous)
			{
				if (previous is HYPipeline)
				{
					hybranch.AddFirstObject(previous as HYPipeline);
				}
			}
			return hybranch;
		}

		protected double m_Resistance;

		protected double m_UnbalanceRate;

		protected double m_Length;

		protected HYMEPObject m_Terminal;

		protected List<int> m_PipelineIds;

		protected int m_TerminalId;

		private string Connect = "-";
	}
}
